package sgg_hsp_data_structures._06_sort;

import java.util.Arrays;
import java.util.Date;

public class ShellSort {

    public static void main(String[] args) {
//        int[] arr = {8, 9, 1, 7, 2, 3, 5, 4, 6, 0};

        int[] arr = ArrUtils.getArr();

//        System.out.println("排序前");
//        System.out.println(Arrays.toString(arr));

        ArrUtils.printCurTime();
//        shellSort(arr);
        shellSort2(arr);
//        shellSort3(arr);
        ArrUtils.printCurTime();
//        System.out.println(Arrays.toString(arr));
        System.out.printf("%tc", new Date());
    }


    /**
     * 交换的代价很高
     *
     * @param arr
     */
    public static void shellSort(int[] arr) {
        int temp = 0;

        for (int gap = arr.length / 2; gap > 0; gap = gap / 2) {
            for (int i = gap; i < arr.length; i++) {

                for (int j = i - gap; j >= 0; j -= gap) {
                    if (arr[j] > arr[j + gap]) {
                        temp = arr[j];
                        arr[j] = arr[j + gap];
                        arr[j + gap] = temp;
                    }
                }
            }
//            System.out.println(Arrays.toString(arr));
        }


        /**
         for (int i = 5; i < arr.length; i++) {

         for (int j = i - 5; j >= 0; j -= 5) {
         if (arr[j] > arr[j + 5]) {
         temp = arr[j];
         arr[j] = arr[j + 5];
         arr[j + 5] = temp;
         }
         }
         }

         System.out.println(Arrays.toString(arr));


         for (int i = 2; i < arr.length; i++) {

         for (int j = i - 2; j >= 0; j -= 2) {
         if (arr[j] > arr[j + 2]) {
         temp = arr[j];
         arr[j] = arr[j + 2];
         arr[j + 2] = temp;
         }
         }
         }

         System.out.println(Arrays.toString(arr));

         for (int i = 1; i < arr.length; i++) {

         for (int j = i - 1; j >= 0; j -= 1) {
         if (arr[j] > arr[j + 1]) {
         temp = arr[j];
         arr[j] = arr[j + 1];
         arr[j + 1] = temp;
         }
         }
         }

         System.out.println(Arrays.toString(arr));
         */


    }


    /**
     * 移位法
     *
     * @param arr
     */
    public static void shellSort2(int[] arr) {
        int temp = 0;

        int j = 0;
        for (int gap = arr.length / 2; gap > 0; gap = gap / 2) {
            for (int i = gap; i < arr.length; i++) {
                j = i;
                temp = arr[j];
                if (arr[j] < arr[j - gap]) {
                    while (j - gap >= 0 && temp < arr[j - gap]) {
                        arr[j] = arr[j - gap];
                        j -= gap;
                    }
                    // 找到 位置
                    arr[j] = temp;
                }
            }
        }
    }

    //对交换式的希尔排序进行优化->移位法
    public static void shellSort3(int[] arr) {

        // 增量gap, 并逐步的缩小增量
        for (int gap = arr.length / 2; gap > 0; gap /= 2) {
            // 从第gap个元素，逐个对其所在的组进行直接插入排序
            for (int i = gap; i < arr.length; i++) {
                int j = i;
                int temp = arr[j];
                if (arr[j] < arr[j - gap]) {
                    while (j - gap >= 0 && temp < arr[j - gap]) {
                        //移动
                        arr[j] = arr[j - gap];
                        j -= gap;
                    }
                    //当退出while后，就给temp找到插入的位置
                    arr[j] = temp;
                }

            }
        }
    }

}
